--
-- Author: ping.wang
-- Date: 2019-06-26 11:56:22
--
-- RewardHeroTreasureBuyExpView
--

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)
local ListViewVertical = require("app.widget.listview.ListViewVertical")
local IconRender = require("app.widget.renders.IconRender")

ClassRef.VIEW_ROOT_RES = Res.RewardHeroTreasureBuyExpScene

--self._data = {RewardHeroTreasureVo,self._coin}
function ClassRef:init()
    self._ui.Button_close:addClickEventListener(function()
        self:closeView()
    end)

    self._heroTreasureData = self._data

    local constConfig = self._heroTreasureData:getConstConfig()
    --这里约定constConfig一定存在 不存在则报错


    self._coin = UD:parseRewardToBagItem(constConfig.expPrice)

    self._suppliesPanel = self._ui.Panel_supplies
    self._buyNum = self._suppliesPanel:findChild("num")
    self._minusBtn = self._suppliesPanel:findChild("Button_minus")
    self._minusBtn:addTouchEventListener(handler(self, self._minusBtnClickEvent))

    self._plusBtn = self._suppliesPanel:findChild("Button_plus")
    self._plusBtn:addTouchEventListener(handler(self, self._plusBtnClickEvent))

    self._needExp = self._heroTreasureData:getCurLvToMaxLvNeedExp()


    self._perExp = checknumber(constConfig.expNum)
    self._curBuyNum = 1 --至少买一份 一份等于50经验

    self._buyNumMax = math.ceil(self._needExp/self._perExp)

    local params = {
        renderClass = IconRender,
        perNum = 4,
        gapY = 6,
        isMultipleLayer = true,
        cellEventProxy = function(eventName, data)

            --executeFunc(self._onOperate, eventName, data)
        end,
        createCellListen = function(cell)
            cell:setTipEnable(true)
            cell:setSelected(false)
        end
    }

    self._productListNode = ListViewVertical.new( self._ui.Panel_content:findChild("ScrollView"), params )

    self._upLv = self._heroTreasureData:getCurLv() --初始化
    self:setBuyExp(self._curBuyNum)


    --购买按钮
    self._ui.Panel_buy:findChild("Button"):addClickEventListener(function()
        --购买的时候需要判断货币是否足够
        if UD:getItemAmountByTid(self._coin:getId()) >= self._curBuyNum then
            executeFunc(self._onOperate, "buyHeroTreasureExp",{activityUID = self._heroTreasureData:getUID(), buyNum = self._curBuyNum})
        else
            if UIHandler:isFunctionOpen(Constant.FUNC_TYPE_SHOP) then
                local goShopDialog = display.newConfirmDialog( L("levelpromotion7"),
                    function() -- 确定
                        app:sendMsg("MallController", "buyGameRes", Constant.ShopDraw_ItemId)
                    end)
                UIHandler:addPopDialog( goShopDialog )
            end
        end
    end)

    self._ui.Panel_buy:findChild("icon"):loadTexture(self._coin:getIcon())
end

--减少单价
function ClassRef:_minusBtnClickEvent(sender,eventType)
    self:_doLongPressClickEvent(sender,eventType,function() self:_minusPrice() end)
end

--增大单价
function ClassRef:_plusBtnClickEvent(sender,eventType)
    self:_doLongPressClickEvent(sender,eventType,function() self:_plusPrice() end)
end

function ClassRef:_doLongPressClickEvent(sender,eventType,callFunc)
    if eventType == ccui.TouchEventType.began then
        self._showTipsFlag = true
        self._delayPressAction = self:performWithDelay(
            function()
                if not self._longPressAction then
                    self._longPressAction = self:schedule(callFunc,0.05)
                end
            end,0.3) --0.3秒后执行回调

    elseif eventType == ccui.TouchEventType.ended or eventType == ccui.TouchEventType.canceled then

        if self._delayPressAction then
            self:stopAction(self._delayPressAction)
            self._delayPressAction = nil
        end

        if self._longPressAction then
            self:stopAction(self._longPressAction)
            self._longPressAction = nil
        else
            callFunc()
        end
        self._showTipsFlag = false
    end
end

--达到上限的时候显示一次tips
function ClassRef:_showTips(str)
    if self._showTipsFlag then
        display.pushToast(str)
        self._showTipsFlag = false
    end
end

function ClassRef:_minusPrice()
    if self._curBuyNum == 1 then
        --弹出提示 若已经弹出过则不用弹了
        self:_showTips(string.format(L("battlepass_string12"),self._perExp))
    else
        local val = self._curBuyNum - 1
        if val == 0 then
            val = 1
        end
        self:setBuyExp(val)
    end
end

function ClassRef:_plusPrice()
    if self._curBuyNum == self._buyNumMax then
        self:_showTips(L("battlepass_string13"))
    else
        local val = self._curBuyNum + 1
        if val >= self._buyNumMax then
            val = self._buyNumMax
        end
        self:setBuyExp(val)
    end
end

function ClassRef:setBuyExp(num)
    self._curBuyNum = num
    self._buyNum:setString(tostring(num * self._perExp))
    self._minusBtn:setTouchEnabled(true)
    self._plusBtn:setTouchEnabled(true)
    display.setBtnGray(self._minusBtn,false)
    display.setBtnGray(self._plusBtn,false)
    if num == 1 then
        display.setBtnGray(self._minusBtn,true)
        self._minusBtn:setTouchEnabled(false)
        if self._longPressAction then
            self:stopAction(self._longPressAction)
            self._longPressAction = nil
        end
        self:_showTips(string.format(L("battlepass_string12"),self._perExp))
    elseif num == self._buyNumMax then
        display.setBtnGray(self._plusBtn,true)
        self._plusBtn:setTouchEnabled(false)
        self:_showTips(L("battlepass_string13"))
        if self._longPressAction then
            self:stopAction(self._longPressAction)
            self._longPressAction = nil
        end
    end

    --当等级有变化的时候则载入数据刷新
    local lv,awards = self._heroTreasureData:getCanLv(self._curBuyNum * self._perExp)
    if lv ~= self._heroTreasureData:getCurLv() then
        self._ui.Panel_content:findChild("text_2"):setVisible(true)
        self._ui.Panel_content:findChild("text_2"):setString(string.format(L("battlepass_string7"),lv))

    else
        self._ui.Panel_content:findChild("text_2"):setVisible(false)
    end

    if lv ~= self._upLv then
        self._upLv = lv
        self._productListNode:reloadData(awards)
    end

    if #awards == 0 then
        self._ui.Panel_content:findChild("text_2"):setVisible(true)
        self._ui.Panel_content:findChild("text_2"):setString(L("battlepass_string15"))
    end

    self._ui.Panel_buy:findChild("num"):setString(self._curBuyNum * self._coin:getAmount())
end

return ClassRef
